import 'package:flutter/material.dart';
import 'package:testlearn/page/homepage.dart';
import 'package:testlearn/page/projectpage.dart';
import 'package:testlearn/page/squarepage.dart';
import 'package:testlearn/page/systempage.dart';
import 'package:testlearn/res/strings.dart';

class BaseHomePage extends StatefulWidget {
  const BaseHomePage({Key? key}) : super(key: key);

  @override
  _BaseHomePageState createState() => _BaseHomePageState();
}

class _BaseHomePageState extends State<BaseHomePage> {
  var pages = [HomePage(), SquarePage(), SystemPage(), ProjectPage()];
  var _pagecontroller = PageController(initialPage: 0);
  int _selectedIndex = 0;
  var title = YStrings.home;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(title),
        leading: GestureDetector(
          onTap: () {
            print("点击菜单");
          },
          child: Icon(Icons.menu),
        ),
        actions: [
          IconButton(
              onPressed: () {
                //到搜索页
              },
              icon: Icon(Icons.search))
        ],
      ),
      body: PageView.builder(
          controller: _pagecontroller,
          onPageChanged: _onpagechange,
          itemCount: pages.length,
          physics: new NeverScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return pages.elementAt(index);
          }),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: YStrings.home),
          BottomNavigationBarItem(
              icon: Icon(Icons.filter_list), label: YStrings.tree),
          BottomNavigationBarItem(
              icon: Icon(Icons.low_priority), label: YStrings.navi),
          BottomNavigationBarItem(
              icon: Icon(Icons.apps), label: YStrings.project),
        ],
        currentIndex: _selectedIndex,
        type: BottomNavigationBarType.fixed,
        fixedColor: Theme.of(context).primaryColor,
        onTap: _onItemTapped,
      ),
    );
  }

  void _onpagechange(int value) {
    setState(() {
      _selectedIndex = value;
      print(value);
    });
  }

  void _onItemTapped(int value) {
    setState(() {
      switch (value) {
        case 0:
          title = YStrings.home;
          break;

        case 1:
          title = YStrings.tree;
          break;

        case 2:
          title = YStrings.navi;
          break;
        case 3:
          title = YStrings.project;
          break;
      }
    });
    //bottomNavigationBar 和 PageView 关联
    _pagecontroller.animateToPage(value,
        duration: Duration(milliseconds: 300), curve: Curves.ease);
  }
}
